﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emoney.UserTraining.Model;

namespace Emoney.UserTraining.DAL
{
    public class BaseDAL
    {
        public static IQueryable<T> Paging<T>(IQueryable<T> query, BaseQuery p)
        {
            p.RecordCount = query.Count();
            if ((p.Index - 1) * p.PageSize > p.RecordCount)
            {
                int maxIndex = p.RecordCount / p.PageSize;
                if (p.RecordCount % p.PageSize > 0)
                {
                    maxIndex++;
                }
                p.Index = maxIndex;
            }
            query = query.Skip((p.Index - 1) * p.PageSize).Take(p.PageSize);
            return query;
        }

        public static IEnumerable<T> Paging<T>(IEnumerable<T> query, BaseQuery p)
        {
            p.RecordCount = query.Count();
            if ((p.Index - 1) * p.PageSize > p.RecordCount)
            {
                int maxIndex = p.RecordCount / p.PageSize;
                if (p.RecordCount % p.PageSize > 0)
                {
                    maxIndex++;
                }
                p.Index = maxIndex;
            }
            query = query.Skip((p.Index - 1) * p.PageSize).Take(p.PageSize);
            return query;
        }
    }
}
